/**
 * @author Aaron
 * @createTime 9:53 2022/7/1
 **/
package api

import (
	"github.com/jinzhu/copier"
	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql/db_main"
)

// @Summary 获取帮助列表
// @Description 获取帮助列表
// @Accept  json
// @Produce  json
// @Tags Help
// @Success 200 {object} api_models.GetHelpListResp "result"
// @Router /api/help/getHelpList [POST]
func (base *Controller) GetHelpList(c echo.Context) error {
	helpList, err := db_main.GetHelpList()
	resp := make([]*api_models.GetHelpListResp, 0)
	for _, Help := range helpList {
		des := &api_models.GetHelpListResp{}
		err := copier.Copy(des, Help)
		if err != nil {
			return util.BuildFail(c, 1, "", err)
		}
		runes := []rune(util.TrimHtml(Help.Content))
		i := 50
		if len(runes) < 50 {
			i = len(runes)
		}
		des.Profile = string(runes[0:i])
		resp = append(resp, des)
	}
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	if len(resp) == 0 {
		return util.BuildSuccess(c, []interface{}{}, "")
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 根据ID获取帮助
// @Description 根据ID获取帮助
// @Accept  json
// @Produce  json
// @Tags Help
// @Param   sendBody body api_models.GetHelpByIdReq true "body"
// @Success 200 {object} api_models.GetHelpByIdResp "result"
// @Router /api/help/getHelpById [POST]
func (base *Controller) GetHelpById(c echo.Context) error {
	r := &api_models.GetHelpByIdReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	Help, err := db_main.GetHelp(r.ID)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	resp := &api_models.GetHelpByIdResp{}
	err = copier.Copy(resp, Help)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}
